<template>
  <div class="container">
    <div class="app-container">
      <div class="box">
        <el-button type="primary" size="mini" @click="dialogFormVisible=true;compile=true">添加角色</el-button>
      </div>
      <template>
        <el-table
          :data="tableData"
          style="width: 100%"
        >
          <el-table-column
            prop="name"
            label="角色"
            width="180"
            align="center"
          />
          <el-table-column prop="state" label="启用" width="180" align="center">
            <template v-slot="{row}"><span>{{ row.state===1?'已启用':'未启用' }}</span></template>
          </el-table-column>
          <el-table-column
            prop="description"
            label="描述"
            align="center"
          />
          <el-table-column label="操作" align="center">
            <template v-slot="{row}"><el-button size="mini" type="text">分配权限</el-button>
              <el-button size="mini" type="text" @click="compileRole(row.id)">编辑</el-button>
              <el-popconfirm
                title="确定要删除吗？"
                @onConfirm="btnremove(row.id)"
              >
                <el-button slot="reference" size="mini" type="text" class="btn">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 页码 -->
      <el-row type="flex" justify="end" align="center" style="height:60px">
        <el-col :span="4">
          <el-pagination
            v-if="total"
            small
            layout="prev, pager, next"
            :current-page="pageAll.page"
            :page-size="pageAll.pagesize"
            :total="total"
            @current-change="change"
          />
        </el-col>
      </el-row>
    </div>
    <!-- 弹出框 -->
    <el-dialog :title=" compile?'添加角色':'编辑角色' " :visible.sync="dialogFormVisible" width="500px" @close="butNoOK">
      <el-form ref="form" label-width="120px" :rules="rules" :model="listRole">
        <el-form-item label="角色" size="mini" style="width:400px" prop="name">
          <el-input v-model="listRole.name" />
        </el-form-item>
        <el-form-item label="启用" size="mini" style="width:400px" prop="state">
          <el-switch
            v-model="listRole.state"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="描述" size="mini" style="width:400px" prop="description">
          <el-input v-model="listRole.description" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" @click="btnOk">确定</el-button>
          <el-button size="mini" @click="butNoOK">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, addRole, detailsRole, modifyRole, removeRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      tableData: [], // 渲染页面用的
      pageAll: { // 分页用的
        page: 1,
        pagesize: 5
      },
      total: '', // 分页总数
      dialogFormVisible: false, // 弹框的开关
      compile: false, // 控制弹出框是编辑用还是添加用
      listRole: { // 新增角色用的
        name: '', // 姓名
        description: '', // 描述信息
        state: ''
      },
      rules: {
        name: [{ required: true, message: '请填写角色名称', trigger: 'blur' }], // 姓名
        description: [{ required: true, message: '请填写角色名称', trigger: 'blur' }] // 描述信息
      }

    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const res = await getRoleList(this.pageAll)
      this.total = res.total
      this.tableData = res.rows
      // console.log(this.tableData, this.total)
    },
    btnOk() { // 点击确定
      this.$refs.form.validate(async isok => {
        if (isok) {
          if (!this.listRole.permIds) {
            await addRole(this.listRole)
            this.$message.success('新增成功')
          } else {
            await modifyRole(this.listRole)
            this.$message.success('修改成功')
          }
          this.getRoleList()
          this.butNoOK()
        }
      })
    },
    butNoOK() { // 点击取消
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
    },
    change(n) {
      this.pageAll.page = n
      this.getRoleList()
    },
    async compileRole(id) {
      this.dialogFormVisible = true
      this.compile = false
      // 现在要把表单数据渲染到页面上 先获取当前id调取现在点的是谁？
      this.listRole = await detailsRole(id)
    },
    async btnremove(id) { // 点击删除按钮
      await removeRole(id)
      this.$message.success('删除成功')
      if (this.tableData.length === 1) this.pageAll.page--
      this.getRoleList()
    }
  }
}

</script>

<style scoped>
.box {
  padding: 10px;

}
.btn{
  margin-left: 10px;
}
</style>
